package it.cst.entity.service;

import java.util.ArrayList;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import it.cst.entity.Match;
import it.cst.entity.dao.MatchDAO;
import it.cst.util.MatchComparator;
import it.cst.util.Paginator;
import it.cst.util.SpringApplicationContext;





@Service
public class MatchServiceImpl implements MatchService{
	@Autowired
	private MatchDAO matchDAO;
	
	public MatchDAO getMatchDAO() {
		return matchDAO;
	}

	public void setMatchDAO(MatchDAO matchDAO) {
		this.matchDAO = matchDAO;
	}

	@Transactional
	public Match addMatch(Match match) {
		Integer id = matchDAO.addMatch(match);
		return matchDAO.getMatch(id);
	}

	@Transactional
	public List<Match> listMatch() {
		return matchDAO.listMatch();
	}

	@Transactional
	public void removeMatch(Match match) {
		matchDAO.removeMatch(match);
	}

	@Transactional
	public Match getMatch(Integer id) {
		return matchDAO.getMatch(id);
	}

	@Transactional
	public Match getLastMatch() {
		return matchDAO.getLastMatch();
	}

	@Transactional
	public Paginator listMatch(Integer num, Integer idMatch, String operator,String orderFlow, String date,String user) {
		Paginator ret = new Paginator();
		UserService us = (UserService) SpringApplicationContext.getBean("userService");
		ret.setAllUserList(us.listUser());
		int totItems = getTotItems(orderFlow,idMatch,operator,date,user);
		ret.setTotItems(totItems);
		if(totItems > num){
			//there is more items than max page items
			if(operator != null){
				//user press next or prev button
				if(operator.equalsIgnoreCase("<"))
				{
					ret.setHasNextItems(true);
					ret.setHasPrevItems(true);
				}		
				else
				{
					ret.setHasNextItems(true);
					ret.setHasPrevItems(true);
				}	
			}
			else{
				//initial search can go only next
				ret.setHasNextItems(true);
				ret.setHasPrevItems(false);
			}
		}
		else{
			//there is less items than max page items
			if(operator != null){
				if(operator.equalsIgnoreCase("<"))
				{
					ret.setHasNextItems(false);
					ret.setHasPrevItems(true);
				}		
				else
				{
					ret.setHasNextItems(true);
					ret.setHasPrevItems(false);
				}	
			}
			else{
				//initial search but can't go next
				ret.setHasNextItems(false);
				ret.setHasPrevItems(false);				
			}
		}
		//when prev must reverse order of items
		List<Match> ml = getItems(num,orderFlow,idMatch,operator,date,user);
		if(operator != null){
			if(operator.equalsIgnoreCase(">")){
				java.util.Collections.sort(ml, new MatchComparator());
			}
		}
		ret.setObjList(ml);
		if(!ml.isEmpty()){
			ret.setFirstItem(ml.get(0));
			ret.setLastItem(ml.get(ml.size()-1));
			ret.setDateFilter(date);
			ret.setUserFilter(user);
		}
		return ret;
	}

	
	
	
	
	
	
	
	
	
	
	
	private List<Match> getItems(Integer num, String orderFlow,Integer idMatch, String operator, String date, String user) {
		List<Match> ml = null;
		List<Match> ret = new ArrayList<Match>();
		if(date != null && user == null){
			if(idMatch != null && operator != null)
				ml = matchDAO.listMatch(num, idMatch, operator, orderFlow,date,user);
			else
				ml = matchDAO.listMatch(num,date,user);
		}
		else if(date == null && user != null)
		{
			if(idMatch != null && operator != null)
				ml = matchDAO.listMatch(num, idMatch, operator, orderFlow,date,user);
			else
				ml = matchDAO.listMatch(num,date,user);
		}	
		else if(date != null && user != null){
			if(idMatch != null && operator != null)
				ml = matchDAO.listMatch(num, idMatch, operator, orderFlow,date,user);
			else
				ml = matchDAO.listMatch(num,date,user);
		}
		else{
			if(idMatch == null && operator == null)
				ml = matchDAO.listMatch(num);
			else
				ml = matchDAO.listMatch(num, idMatch, operator, orderFlow);
			
		}
		ret.addAll(ml);
		return ret;
	}

	
	
	
	
	private int getTotItems(String orderFlow,Integer idMatch, String operator, String date, String user) {
		int totItems = 0;
		if(date == null && user == null){
			return totItems = countMatch(idMatch,operator);
		}
		else if(date != null && user == null){
			if(idMatch != null && operator != null)
				totItems = matchDAO.countMatch(idMatch, operator, orderFlow,date,user);
			else
				totItems = matchDAO.countMatch(date,user);
		}
		else if(date == null && user != null){
			if(idMatch != null && operator != null)
				totItems = matchDAO.countMatch(idMatch, operator, orderFlow,date,user);
			else
				totItems = matchDAO.countMatch(date,user);
		}		
		else if(date != null && user != null){
			if(idMatch != null && operator != null)
				totItems = matchDAO.countMatch(idMatch, operator, orderFlow,date,user);
			else
				totItems = matchDAO.countMatch(date,user);
				
		}
		return totItems;
	}

	
	
	
	@Transactional
	public Integer countMatch(Integer idMatch, String operator) {
		Integer ret = null;
		if(idMatch == null && operator == null)
			ret = matchDAO.countMatch();
		else
			ret = matchDAO.countMatch(idMatch, operator);
		return ret;
	}

}
